//
//  LGSetNewPasswordController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/14.
//  设置新密码 -- 忘记密码、修改密码

#import "LGSetNewPasswordController.h"

#import "LGAccountInputView.h"
@interface LGSetNewPasswordController ()<UITextFieldDelegate>

/**   */
@property (nonatomic , strong)LGAccountInputView *topView;
/**   */
@property (nonatomic , strong)LGAccountInputView *bottomView;
/**   */
@property (nonatomic , strong)UIButton *sureBtn;

@end

@implementation LGSetNewPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"设置新密码";
    [self.view addSubview:self.topView];
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.sureBtn];
}

//确定
-(void)sureBtnAction{
    if(![self.topView.contenTF.text isEqualToString:self.bottomView.contenTF.text]){
        [TooltipView showMessage:@"两次输入的密码不一致" offset:0];
        return;
    }else if ([self checkPassword] == YES){
        [RequestUtil withPOST:@"/doctor/my.setting/setPassword" parameters:@{@"password":self.topView.contenTF.text} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                if(self.backVC){
                    [self.navigationController popToViewController:self.backVC animated:YES];
                }else{
                    [self.navigationController popToRootViewControllerAnimated:YES];
                }
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            [TooltipView showMessage:LGErrorMessage offset:0];
        }];
    }
}


-(BOOL)checkPassword{
    NSString *content = self.topView.contenTF.text;
    NSArray *numArray = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
    NSArray *letterArray = @[@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"];
    BOOL containNum = NO;
    BOOL containLetter = NO;
    for (NSInteger i = 0; i<content.length; i++) {
        NSString *letter = [content substringWithRange:NSMakeRange(i, 1)];
        if([numArray containsObject:letter]){
            containNum = YES;
        }
        if ([letterArray containsObject:letter]){
            containLetter = YES;
        }
        if(containNum == YES && containLetter == YES) break;
    }

    if(containNum == NO){
        [TooltipView showMessage:@"密码不能全是字母" offset:0];
        return NO;
        
    }else if (containLetter == NO){
        [TooltipView showMessage:@"密码不能全是数字" offset:0];
        return NO;
    }
    return YES;
}

-(LGAccountInputView *)topView{
    if(!_topView){
        _topView = [[LGAccountInputView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(24), Screen_W-viewPix(32), viewPix(56))];
        [_topView setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:10];
        _topView.backgroundColor = [UIColor whiteColor];
        _topView.type = @"password";
        _topView.contenTF.attributedPlaceholder = [@"请输入6-20位数字和字母组合的密码" attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#999999"]}];
        _topView.contenTF.delegate = self;
        _topView.hasBorder = NO;
    }
    return _topView;
}

-(LGAccountInputView *)bottomView{
    if(!_bottomView){
        _bottomView = [[LGAccountInputView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.topView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(56))];
        [_bottomView setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:10];
        _bottomView.backgroundColor = [UIColor whiteColor];
        _bottomView.type = @"password";
        _bottomView.contenTF.attributedPlaceholder = [@"请再次输入密码" attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#999999"]}];
        _bottomView.contenTF.delegate = self;
        _bottomView.hasBorder = NO;
    }
    return _bottomView;
}

-(UIButton *)sureBtn{
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithTitle:@"确定" topMargin:kMaxY(self.bottomView.frame)+viewPix(140)];
        [_sureBtn addTarget:self action:@selector(sureBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
